@extends('admin.common.base-master')

@section('html-head')
    <link href="{{url('css/admin/pagenum.css')}}" rel="stylesheet" />
@endsection

@section('html-body')
    <!-- content start -->
    <div id="content">
        <!-- cur-position start -->
        <div id="cur-position">
            <span class="tit">当前位置：</span>
            <a href="#">用户管理</a>
        </div>
    @include('admin.common.users-header')
        <!-- cur-position end -->
        <div class="sx">
            <input type="text" class="gg-ipt" style="width: 130px;height: 26px" placeholder="输入昵称或手机号" name="keyword" id="keyword" value="{{$keyword}}">
            <button class="sx-btn" id="sx_btn">筛选</button>
        </div>
        <div class="table-box">
            <table class="table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>头像</th>
                    <th>昵称</th>
                    <th>手机号</th>
                    <th>地区</th>
                    <th>性别</th>
                    <th>余额</th>
                    <th>折扣</th>
                    <th>创建时间</th>
                    <th>账户状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="fansinfo">
                @if(count($users) > 0)
                    @foreach($users as $user)
                        <tr>
                            <td>{{$user->id}}</td>
                            <td class="account-td" style="width: 20px;">
                        <span class="photo">
                            @if($user->userInfo)<img src="{{\App\Http\Services\FileManager::getImgUrl($user->userInfo->photo_id)}}" alt="">@endif
                        </span>
                            </td>
                            <td>
                                <div class="info-box">
                                    <p>
                                        <span class="icon icon-rzh"></span>
                                        <span class="name">@if($user->userInfo){{$user->userInfo->nickname}}@endif</span>
                                    </p>
                                </div>
                            </td>
                            <td>{{$user->phone}}</td>
                            <td>@if($user->userInfo){{$user->userInfo->province}}{{$user->userInfo->city}}{{$user->userInfo->area}}@endif</td>
                            <td>
                                @if($user->userInfo)
                                    @if($user->userInfo->sex == 1)
                                        男
                                    @else
                                        女
                                    @endif
                                @endif
                            </td>
                            <td>
                                @if(isset($user->charge_available))
                                    {{$user->charge_available}}
                                @else
                                    0
                                @endif
                            </td>
                            <td>{{$user->discount_rule_name}}</td>
                            <td>{{$user->created_at}}</td>

                            <td>@if((int)$user->status == 0)<span class="color3">锁定</span> @elseif((int)$user->status == 1)<span class="color4">正常</span>@endif</td>
                            <td>
                                <a class="color2" style="cursor: pointer" title="点击充值" onclick="showBox({{$user->id}},{{$user->discount_id}})">充值</a>
                                <a class="color2" style="cursor: pointer" title="设置折扣" onclick="showBoxDiscount({{$user->id}},{{$user->discount_id}})">设置折扣</a>
                                <a class="color2" target="_blank" href="{{route('Admin.Home.getUserChargeRecord').'?user_id='.$user->id}}">余额记录</a></td>
                        </tr>
                    @endforeach
                @endif
                </tbody>
            </table>
            <?php echo $users->appends(['keyword'=>$keyword])->render(); ?>
        </div>
        <div class="upPop-box">
            <div class="backdrop"></div>
            <div id="upPop" class="info-pop tixian" style="position: relative;">
{{--                <input type="hidden" name="id" id="classifyid" value=""/>--}}
                <a class="close" href="javascript:void(0);"></a>
                <h3>充值</h3>

                <div class="fill-list" style="overflow: auto;">
                    <div>请输入充值金额
                        <input type="hidden" name="id" id="id" value="0">
                        <input type="text" name="amount" id="amount" autocomplete="off" value="">
                    </div>
                </div>

                <div class="fill-list" style="overflow: auto;">
                    <div>备注
                        <input type="text" name="remark" id="remark" value="">
                    </div>
                </div>
{{--                <div class="fill-list" style="overflow: auto;">--}}
{{--                    <div>请选择折扣方案--}}
{{--                        <select name="discount" id="discount" value="">--}}
{{--                            <option value="0">无优惠</option>--}}
{{--                            @if(count($discount) > 0)--}}
{{--                                @foreach($discount as $item)--}}
{{--                                    <option value="{{$item->id}}">{{$item->rule_name}}</option>--}}
{{--                                @endforeach--}}
{{--                            @endif--}}
{{--                        </select>--}}
{{--                    </div>--}}
{{--                </div>--}}
                <div class="fill-list" style="position: absolute;bottom: 5px">
                    <span class="fill-tit">&nbsp;</span>
                    <input class="fill-submit" type="button" id="subThisForm" value="提交">
                </div>
            </div>
        </div>
        <div class="upPop-box-discount">
            <div class="backdrop"></div>
            <div id="upPop" class="info-pop tixian" style="position: relative;">
{{--                <input type="hidden" name="id" id="classifyid" value=""/>--}}
                <a class="close-discount" href="javascript:void(0);"></a>
                <h3>设置折扣</h3>
                <div class="fill-list" style="overflow: auto;">
                    <input type="hidden" name="uid" id="uid" value="0">
                    <div>请选择折扣方案
                        <select name="discountid" id="discountid" value="">
                            <option value="0">无优惠</option>
                            @if(count($discount) > 0)
                                @foreach($discount as $item)
                                    <option value="{{$item->id}}">{{$item->rule_name}}</option>
                                @endforeach
                            @endif
                        </select>
                    </div>
                </div>
                <div class="fill-list" style="position: absolute;bottom: 5px">
                    <span class="fill-tit">&nbsp;</span>
                    <input class="fill-submit" type="button" id="subThisForm-discount" value="提交">
                </div>
            </div>
        </div>
    </div>
    <!-- content end -->
    <script>
        function showBox(id,discount_id){
            $('.upPop-box').upPop();
            $("#id").val(id);
            // $("#discount").val(discount_id);
        }
        function showBoxDiscount(id,discount_id){
            $('.upPop-box-discount').upPop();
            $("#uid").val(id);
            $("#discountid").val(discount_id);
        }

        $(document).on('click', '#subThisForm', function () {
            var id = $('#id').val();
            var amount = $('#amount').val();
            var remark = $('#remark').val();
            // var discount_id   = $('#discount option:selected').val();

            $.ajax({
                url: '{{route("Api.Admin.sys.addChargeRecord")}}',
                type: 'post',
                dataType: 'json',
                data: {
                    user_id: id,
                    amount:amount,
                    remark:remark,
                    // discount_id:discount_id
                },
                success: function (obj) {
                    if (obj.level == "success") {
                        noty({
                            text: obj.message, type: obj.level, timeout: 600,
                            callback: {
                                afterClose: function () {
                                    window.location.reload();
                                }
                            }
                        });
                    } else if (obj.level == "error") {
                        noty({text: obj.message, type: obj.level, timeout: 600});
                    }

                },
                error: function (obj) {
                    console.log(obj);
                }
            });
        });
        $(document).on('click', '#subThisForm-discount', function () {
            var id = $('#uid').val();
            var discount_id   = $('#discountid option:selected').val();

            $.ajax({
                url: '{{route("Api.Admin.sys.setUserDiscount")}}',
                type: 'post',
                dataType: 'json',
                data: {
                    user_id: id,
                    discount_id:discount_id
                },
                success: function (obj) {
                    if (obj.level == "success") {
                        noty({
                            text: obj.message, type: obj.level, timeout: 600,
                            callback: {
                                afterClose: function () {
                                    window.location.reload();
                                }
                            }
                        });
                    } else if (obj.level == "error") {
                        noty({text: obj.message, type: obj.level, timeout: 600});
                    }

                },
                error: function (obj) {
                    console.log(obj);
                }
            });
        });
        $('#sx_btn').click(function () {
            var keyword = $('#keyword').val();
            var projectId = $('#project_id option:selected').val();
            var status = $('#status option:selected').val();
            var startTime = $('#start_time').val();
            var endTime = $('#end_time').val();
            var editToken = $('#editToken').val();

            window.location.href = "{{route('Admin.Home.userList')}}?keyword=" + keyword;
        });
    </script>

@endsection